package org.jeecg.modules.bus.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.bus.entity.BusProInfTrackData;
import org.jeecg.modules.bus.entity.BusProjectBasic;
import org.jeecg.modules.bus.entity.BusRiskProVO;
import org.jeecg.modules.bus.entity.BusRiskanalysisData;
import org.jeecg.modules.bus.service.IBusProInfTrackDataService;
import org.jeecg.modules.bus.service.IBusProjectBasicService;
import org.jeecg.modules.bus.service.IBusRiskanalysisDataService;
import org.jeecg.modules.bus.entity.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 商务管理(项目风险分析)数据表
 * @Author: jeecg-boot
 * @Date:   2022-11-07
 * @Version: V1.0
 */
@Api(tags="商务管理(项目风险分析)数据表")
@RestController
@RequestMapping("/bus/busRiskanalysisData")
@Slf4j
public class BusRiskanalysisDataController extends JeecgController<BusRiskanalysisData, IBusRiskanalysisDataService> {
	@Autowired
	private IBusRiskanalysisDataService busRiskanalysisDataService;
	@Autowired
	private IBusProInfTrackDataService busProInfTrackDataService;
	@Autowired
	private IBusProjectBasicService busProjectBasicService;
	/**
	 * 分页列表查询
	 *
	 * @param busRiskanalysisData
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商务管理(项目风险分析)数据表-分页列表查询")
	@ApiOperation(value="商务管理(项目风险分析)数据表-分页列表查询", notes="商务管理(项目风险分析)数据表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BusRiskanalysisData busRiskanalysisData,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusRiskanalysisData> queryWrapper = QueryGenerator.initQueryWrapper(busRiskanalysisData, req.getParameterMap());
		Page<BusRiskanalysisData> page = new Page<BusRiskanalysisData>(pageNo, pageSize);
		IPage<BusRiskanalysisData> pageList = busRiskanalysisDataService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  *   选择最新信息跟踪
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "商务管理(项目风险分析)数据表-选择最新信息跟踪")
	 @ApiOperation(value="商务管理(项目风险分析)数据表-选择最新信息跟踪", notes="商务管理(项目风险分析)数据表-选择最新信息跟踪")
	 @GetMapping(value = "/listInfo")
	 public Result<?> queryListInfo(BusProInfTrackData busProInfTrackData,
                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                    HttpServletRequest req) {
		 // 1.先获取所有的最新的信息跟踪
		 // List<BusProInfTrackData> busProInfTrackDataList = busProInfTrackDataService.queryNewInfo();
		 // 2.筛选过没有被选择的信息跟踪
		 // 3.返回 pageList
		 QueryWrapper<BusProInfTrackData> queryWrapper = new QueryWrapper<>();
		 queryWrapper.inSql("create_time", "SELECT MAX(l.create_time) as create_time FROM bus_pro_inf_track_data l GROUP BY l.pro_id");
		 QueryGenerator.installMplus(queryWrapper,busProInfTrackData, req.getParameterMap());

		 Page<BusProInfTrackData> page = new Page<>(pageNo, pageSize);
		 // pageList.setRecords(busProInfTrackDataList);
		 IPage<BusProInfTrackData> pageList = busProInfTrackDataService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param busRiskProVO
	 * @return
	 */
	@AutoLog(value = "商务管理(项目风险分析)数据表-添加")
	@ApiOperation(value="商务管理(项目风险分析)数据表-添加", notes="商务管理(项目风险分析)数据表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BusRiskProVO busRiskProVO) {
		busRiskanalysisDataService.save(busRiskProVO.getBusRiskanalysisData());
		String id = busRiskProVO.getBusRiskanalysisData().getId();
		List<BusProjectBasic> busProjectBasicList = busRiskProVO.getBusProjectBasicList();
		for (BusProjectBasic busProjectBasic : busProjectBasicList){
			busProjectBasic.setRiskAnalysisId(id);
			busProjectBasicService.save(busProjectBasic);
		}
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param busRiskanalysisData
	 * @return
	 */
	@AutoLog(value = "商务管理(项目风险分析)数据表-编辑")
	@ApiOperation(value="商务管理(项目风险分析)数据表-编辑", notes="商务管理(项目风险分析)数据表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BusRiskanalysisData busRiskanalysisData) {
		busRiskanalysisDataService.updateById(busRiskanalysisData);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理(项目风险分析)数据表-通过id删除")
	@ApiOperation(value="商务管理(项目风险分析)数据表-通过id删除", notes="商务管理(项目风险分析)数据表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		busRiskanalysisDataService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商务管理(项目风险分析)数据表-批量删除")
	@ApiOperation(value="商务管理(项目风险分析)数据表-批量删除", notes="商务管理(项目风险分析)数据表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busRiskanalysisDataService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理(项目风险分析)数据表-通过id查询")
	@ApiOperation(value="商务管理(项目风险分析)数据表-通过id查询", notes="商务管理(项目风险分析)数据表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BusRiskanalysisData busRiskanalysisData = busRiskanalysisDataService.getById(id);
		if(busRiskanalysisData==null) {
			return Result.error("未找到对应数据");
		}
		QueryWrapper<BusProjectBasic> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("risk_analysis_id",id);
		List<BusProjectBasic> busProjectBasicList = busProjectBasicService.list(queryWrapper);
		BusRiskProVO busRiskProVO = new BusRiskProVO();
		busRiskProVO.setBusRiskanalysisData(busRiskanalysisData);
		busRiskProVO.setBusProjectBasicList(busProjectBasicList);
		return Result.OK(busRiskProVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busRiskanalysisData
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusRiskanalysisData busRiskanalysisData) {
        return super.exportXls(request, busRiskanalysisData, BusRiskanalysisData.class, "商务管理(项目风险分析)数据表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusRiskanalysisData.class);
    }

}
